package document

import (
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
)

// FileInfoHandler 文件信息处理器
type FileInfoHandler struct {
	service *FileInfoService
}

// NewFileInfoHandler 创建文件信息处理器
func NewFileInfoHandler(service *FileInfoService) *FileInfoHandler {
	return &FileInfoHandler{service: service}
}

// CreateFileInfo 创建文件信息
func (h *FileInfoHandler) CreateFileInfo(c *gin.Context) {
	var req CreateFileInfoRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误: " + err.Error()})
		return
	}

	response, err := h.service.CreateFileInfo(&req)
	if err != nil {
		log.Errorf("创建文件信息失败: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    response,
		"message": "文件信息创建成功",
	})
}

// GetFileInfo 获取文件信息
func (h *FileInfoHandler) GetFileInfo(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的ID参数"})
		return
	}

	response, err := h.service.GetFileInfo(id)
	if err != nil {
		log.Errorf("获取文件信息失败: %v", err)
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    response,
	})
}

// UpdateFileInfo 更新文件信息
func (h *FileInfoHandler) UpdateFileInfo(c *gin.Context) {
	var req UpdateFileInfoRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误: " + err.Error()})
		return
	}

	response, err := h.service.UpdateFileInfo(&req)
	if err != nil {
		log.Errorf("更新文件信息失败: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    response,
		"message": "文件信息更新成功",
	})
}

// DeleteFileInfo 删除文件信息
func (h *FileInfoHandler) DeleteFileInfo(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的ID参数"})
		return
	}

	err = h.service.DeleteFileInfo(id)
	if err != nil {
		log.Errorf("删除文件信息失败: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"message": "文件信息删除成功",
	})
}

// ListFileInfos 获取文件信息列表
func (h *FileInfoHandler) ListFileInfos(c *gin.Context) {
	var req ListFileInfoRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误: " + err.Error()})
		return
	}

	response, err := h.service.ListFileInfos(&req)
	if err != nil {
		log.Errorf("获取文件信息列表失败: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    response,
	})
}

// DeleteFileInfoByName 根据文件名删除文件信息
func (h *FileInfoHandler) DeleteFileInfoByName(c *gin.Context) {
	fileName := c.Query("file_name")
	if fileName == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "文件名不能为空"})
		return
	}

	err := h.service.DeleteFileInfoByName(fileName)
	if err != nil {
		log.Errorf("根据文件名删除文件信息失败: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"message": "文件信息删除成功",
	})
}
